#include "Compiler_Run.hpp"
#include "../comm/httplib.h"

using namespace ns_cr;
using namespace httplib;

void Usage(char *arg)
{
    std::cerr << arg << "\t\n"
              << "+ port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }

    EnableCout();

    Server sever;
    sever.Post("/Compiler_Run", [](const Request &req, Response &resp)
               {
        std::string in_json = req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            Compiler_Run::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8");
        } });

    sever.listen("0.0.0.0", atoi(argv[1]));

    return 0;

    // Json::Value user_value;
    // // R"()", raw string
    // user_value["code"] = R"(#include <iostream>
    // int main(){
    //     std::cout << "我就是太阳" << std::endl;
    //     return 0;
    // })";
    // user_value["input"] = "";
    // user_value["cpu_rlimit"] = 1;
    // user_value["mem_rlimit"] = 1024 * 30;// 单位: KB  1024KB = 1M

    // Json::StyledWriter writer;
    // //将用户数据序列化
    // std::string in_json = writer.write(user_value);

    // std::cout << in_json <<std::endl;

    // //把sever端输出内容转到outstr中
    // std::string outstr;
    // Compiler_Run::Start(in_json, &outstr);

    // std::cout << outstr << std::endl;

    // return 0;
}